package com.example.appclient;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import android.util.Log;

public class TCPClient {



    private String path;
    final String HOST = "192.168.0.4";
    public static final int SERVERPORT = 9040;

	    PrintWriter out;
	    BufferedReader in;
	    DataOutputStream dataOutputStream = null;
	    private OnMessageReceived mMessageListener = null;
	    //OutputStream os;
	    public TCPClient(OnMessageReceived listener) {
	        mMessageListener = listener;
	    }

	    public void enviar_a_B(String texto) {
	        try {
	            // TODO add your handling code here:
	            Socket sc;
	            DataOutputStream mensaje;
	            DataInputStream entrada;
	            sc = new Socket(HOST, SERVERPORT);
	            mensaje = new DataOutputStream(sc.getOutputStream());
	            entrada = new DataInputStream(sc.getInputStream());
	            byte[] data = texto.getBytes();
	            mensaje.write(data, 0, data.length);
	            byte[] raw = new byte[3 * 1024];
	            entrada.read(raw, 0, raw.length);
	            String respuesta = new String(raw);
	            sc.close();

	        } catch (IOException ex) {
	            Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
	        }
	    }
	    
	    public void envio() {
	        File archivo = new File(path);
	        if (archivo.exists()) {
	            BufferedReader bR;
	            FileReader tem;
	            StringBuilder stringA = new StringBuilder();
	            String aux;
	            try {
	                tem = new FileReader(archivo);
	                bR = new BufferedReader(tem);
	                aux = bR.readLine();
	                while (aux != null) {
	                    stringA.append(aux);
	                    aux = bR.readLine();
	                }
	                enviar_a_B(stringA.toString());
	                //jTextArea1.setText(stringA.toString());
	                bR.close();
	                tem.close();
	            } catch (IOException ex) {
	                Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
	            }
	        } else {
	          //  lb_estado.setText("Estado: no se encontro el archivo " + path);
	        }
	    }

	   /* public void show_conector(String path) {
	        lb_archivo.setText("Archivo Enviado: "+path);
	        this.setVisible(true);
	        this.path = path;
	        envio();

	    }*/



	    public interface OnMessageReceived {
	        public void messageReceived(String message);
	    }

}
